// JavaScript Document

$.fn.yhj_mywin=function(position){
		if(position&&position instanceof Object){
			//获取用户给定的位置
			var positionLeft=position.left;
			var positionTop=position.top;
			var currentWin=this;//要显示的层对象
			
			var left;
			var top;
			
			var reg=/(px)/ig;//包含px    正则表达式
			
			positionLeft=positionLeft.replace(reg,"");
			positionTop=positionTop.replace(reg,"");
			
			if(!isNaN(positionLeft)){
				positionLeft=parseInt(positionLeft);	
			}
			if(!isNaN(positionTop)){
				positionTop=parseInt(positionTop);	
			}
			
			//可视区域的大小
			var width;
			var height;
			var scrollTop;
			var scrollLeft;
			
			function getWindow(){
				 width=$(window).width();
				 height=$(window).height();
				 scrollTop=$(window).scrollTop();
				 scrollLeft=$(window).scrollLeft();
			}
			//要显示的层的大小	
			var mywidth=currentWin.outerWidth(true);
			var myheight=currentWin.outerHeight(true);
			
			function getLeft(){
				if(positionLeft!=""&&typeof(positionLeft)=="string"){
					if(positionLeft=="left"){
						left=scrollLeft;
					}else if(positionLeft=="center"){
						left=(width-mywidht)/2+scrollLeft;
					}else if(positionLeft=="right"){
						left=width-mywidth+scrollLeft;
					}else{
						left=(width-mywidth)/2	+scrollLeft;
					}
				}else if(positionLeft!=""&&typeof(positionLeft)=="number"){
					left=positionLeft+scrollLeft;
				}else{
					left=scrollLeft;	
				}
			}
			
			function getTop(){
				if(positionTop!=""&&typeof(positionTop)=="string"){
					if(positionTop=="top"){
						top=scrollTop;
					}else if(positionTop=="center"){
						top=(height-myheight)/2+scrollTop;
					}else if(positionTop=="bottom"){
						top=height-myheight+scrollTop;
					}else{
						top=(height-myheight)/2+scrollTop;
					}
				}else if(positionTop!=""&&typeof(positionTop)=="number"){
					top=positionTop+scrollTop;
				}else{
					top=scrollTop;	
				}
			}
			getWindow();
			getLeft();
			getTop();
			currentWin.css({top:top,left:left}).fadeIn("slow");
			
			$(window).resize(function(){
				getWindow();
				getLeft();
				getTop();
				currentWin.animate({left:left,top:top},200);	
			});
			$(window).scroll(function(){
				getWindow();
				getLeft();
				getTop();
				currentWin.css({top:top,left:left});	
			});
			
			this.children(".title").children("img").click(function(){
				currentWin.slideUp("slow");
			});
		}
	}